// pages/user/renew/renew.js
var app = getApp();
var util = require("../../../utils/util.js");
var mta = require('../../../utils/mta_analysis.js');
var half_img=""
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: '',    // 要缴费的信息
    code: '',//户号
    contractInfo: {},
    coupon:[],//返回的优惠券ID集合
    renew_money:0.00,
    coupon_number:0,
    totalMoney: 0,//计算优惠券抵消后的总金额
    payWay:1,//1 微信支付 2对公支付
    openData:{},//对公信息
    all_url:'',//当前凭证
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    mta.Page.init();
    var code =  options.code ? options.code:'';
    //二维码进入获取参数
    if (util.isExitsVariable(options.scene)) {
      var scene = util.sceneToArr(decodeURIComponent(options.scene));
      if (util.isExitsVariable(scene.code)) {
        code = scene.code;
      }
    }
    if (options.q != undefined) {
      var scan_url = decodeURIComponent(options.q); //字符分割//console.log(scan_url); //打印出url//分割网址，提取出参数
      var surls = scan_url.split("code=");
      code = surls[1];
    }
    that.setData({
      code,all_url: '',
    })
    half_img = ''; //置空
    that.getFeeInfo();
    that.companyAccountConfig();
  },
  // 获取要缴费信息
  getFeeInfo: function () {
    var that = this;
    // ajax请求
    app.ajax({
      url: 'User/Pay/getFeeInfo',
      data: { code: that.data.code },
      success: function (res) {
        if (res.data.code == 1000) {
          // 数据赋值
          that.setData({
            detail: res.data.data,
            totalMoney: res.data.data.total_money
          })
        }
      }
    })
  },
  //获取对公信息
  companyAccountConfig: function () {
    var that = this;
    // ajax请求
    app.ajax({
      url: 'Common/Common/companyAccountConfig',
      data: { },
      success: function (res) {
        if (res.data.code == 1000) {
          // 数据赋值
          that.setData({
            openData:res.data.data,
          })
        }
      }
    })
  },
  // 立即缴费
  payment: function (e) {
    var that = this;
    let {code,coupon,payWay,all_url}=that.data;
    if(payWay==2&&half_img==''){
      app.showToast("请上传凭证");
      return false
    }
    let  data={
      code,
      coupon_id:coupon,
      pay_type:payWay,
      payment_voucher:payWay==2?half_img:''
    }
    // ajax请求添加续费订单
    app.ajax({
      url: 'User/Pay/feePay',
      data,
      success: function (res) {
        if (res.data.code == 1000&&payWay==1) {
          // wx.navigateTo({
          //   url: '../../user/payment/payment?order_id=' + res.data.data.renew_order_id + '&type=2' + '&contract_id=' + that.data.contract_id,
          // })
          var jsapi = res.data.data;
          wx.requestPayment({
            timeStamp: jsapi.timeStamp,
            nonceStr: jsapi.nonceStr,
            package: jsapi.package,
            signType: jsapi.signType,
            paySign: jsapi.paySign,
            'success': function (res) {
              app.showToast("支付成功");
              setTimeout(()=>{
                that.setData({
                  all_url: '',
                })
                half_img = ''; //置空
                wx.redirectTo({
                  url: '/pages/guidance/guidance?code=' + code,
                })
              },1000)
            },
            'fail': function (res) {
              app.showToast("支付失败");
            }
          })
        } else if (res.data.code == 1000 && payWay==2){
          app.showToast('提交成功!');
          setTimeout(() => {
            that.setData({
              all_url: '',
            })
            half_img = ''; //置空
            wx.redirectTo({
              url: '/pages/guidance/guidance?code=' + code,
            })
          }, 1000)
        }else if (res.data.code == 1001){
          app.showToast("支付成功");
          setTimeout(() => {
            wx.redirectTo({
              url: '/pages/guidance/guidance?code=' + code,
            })
          }, 1000)
        }else{
          app.showToast(res.data.message);
        }
      }
    })
  },
  //跳转缴费记录
  paymentRecord(){
    wx.navigateTo({
      url: '/pages/guidance/guidance?code=' + this.data.code,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    console.log("coupon获取", that.data.coupon)
    that.getCouponList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  //优惠券
  goCoupon(){
    var couponStr = this.data.coupon.join('#');
    wx.navigateTo({
      url: '/pages/user/econtractCoupon/econtractCoupon?coupon=' + couponStr,
    })
  },
  // 获取我的优惠券列表
  getCouponList() {
    var that = this;
    // ajax请求
    app.ajax({
      url: 'User/Renew/getCoupon',
      data: {
        page: 1,
        row: 1,
        status:1
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            coupon_number: res.data.data.page.total,
          })
        }
      }
    })
  },
  //返回首页
  goHome(){
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  chosePay(e){
      let {way}=e.currentTarget.dataset;
      this.setData({
        payWay:way
      })
  },
   //大图预览
   previewImg(e){
    wx.previewImage({
      urls: [e.currentTarget.dataset.url] // 需要预览的图片http链接列表
    })
  },
  //选择本地图片
  phoneImg() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album','camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        const newUrl = tempFilePaths[0];
        that.uplodImg(newUrl);
      },
      fail(err){
        that.setData({
          all_url: '',
        })
        half_img = ''; //置空
      }
    })
  },
      //上传图片到服务器
  uplodImg(chengimg) {
    var that = this;
    wx.showLoading({
      title: '上传图片中',
    })
    wx.uploadFile({
      url: app.globalData._url + 'Common/Common/upload',
      filePath: chengimg,
      name: 'file',
      success: function (result) {
        if (result.statusCode !== 200) {
          wx.hideLoading();
          app.showToast('上传失败,请重新上传');
          return
        }
        var data = JSON.parse(result.data);
        if (data.code == 1000) {
          half_img = data.data.url;
          that.setData({
            all_url: data.data.all_url,
          })
        } else {
          that.setData({
            all_url: '',
          })
          half_img = ''; //置空
          app.showToast("上传失败,请重新上传");
        }
        wx.hideLoading();
      },
      fail: function() {
        that.setData({
          all_url: '',
        })
        half_img = ''; //置空
        wx.hideLoading();
        app.showToast('上传失败,请重新上传');
      }
    })
  },
})
